package handler

import (
	"360video/video-api/api/reponse"
	"360video/video-api/api/request"
	"360video/video-api/pkg"
	"360video/video-srv/handler/config"
	"github.com/gin-gonic/gin"
	"math/rand"
	"strconv"
	"time"
)

func SendSms(c *gin.Context) {
	req := request.SendSms{}
	if c.ShouldBind(&req) != nil {
		reponse.Error(c, "绑定失败")
		return
	}
	code := rand.Intn(900000) + 100000
	mq := pkg.NewRabbitMQSimple("sendSmsMq")
	mq.PublishSimple(strconv.Itoa(code))
	get := config.Rdb.Get(config.Ctx, "sendSms"+req.Account)
	if get.Val() >= "3" {
		reponse.Error(c, "同一手机号5分钟内最多发送3条验证码")
		return
	}
	//sms, err := pkg.SendSms(req.Account, strconv.Itoa(code))
	//if err != nil {
	//	return
	//}
	//if *sms.Body.Code != "OK" {
	//	reponse.Error(c, *sms.Body.Code)
	//}
	config.Rdb.Incr(config.Ctx, "sendSms"+req.Account)
	config.Rdb.Expire(config.Ctx, "sendSms"+req.Account, time.Minute*5)
	config.Rdb.Set(config.Ctx, "sendSms"+req.Account, code, time.Minute*5)
	reponse.Success(c, "发送成功", code)
}
